program turn_fee_5x5
    
      character*6 ticker
      character*8 cusp
      integer(kind=8):: navail,ntot,numshrs
      dimension nep(5500),favg(5500),price(5500),iperm(5500),nms(100000),ndate(100000,52),factr(100000,52)
      dimension jdate(5500),turno(5500),avgt(5,5),avgp(5,5),avgf(5,5),cnt(5,5)
    
!      open(unit=10,file='e:\FIS_duration\episode_data_type_total.txt')
!      open(unit=11,file='e:\FIS_duration\splits.prn')
!      open(unit=99,file='e:\FIS_duration\episode_5x5_turn_fee_price.txt')
      open(unit=10,file='e:\FIS_duration\episode_data_type_total_rep.txt')
      open(unit=11,file='e:\FIS_duration\splits.prn')
      open(unit=99,file='e:\FIS_duration\episode_5x5_turn_fee_price_rep.txt')
      
      nms=0
      do 700 k=1,3587
      read(11,1100)kprm,kdate,fct
 !     write(6,1100)kprm,kdate,fct
1100  format(1x,i5,1x,i8,1x,f9.5)          
      nms(kprm)=nms(kprm)+1
      ndate(kprm,nms(kprm))=kdate
      factr(kprm,nms(kprm))=fct
700   continue      
    
      iend=0
      i=0
1     i=i+1
2     read(10,1000,end=900)nep(i),ntype,jast,jyr,jmn,jdy,cusp,ticker,itickt,numshrs,age,favg(i),fmax,fmin,fstd,iperm(i),price(i),ivol,retrn,ishrout,vwret,util,navail,ntot
!      write(6,1000)nep(i),ntype,jast,jdate,cusp,ticker,itickt,numshrs,age,favg(i),fmax,fmin,fstd,iperm,price(i),ivol(i),retrn,ishrout(i),vwret,util,navail,ntot
1000 format(1x,i4,1x,i1,1x,i8,1x,i4,2i2,1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13,1x,i12)
      turno(i)=float(ivol)/(1000*float(ishrout))
      jdate(i)=jyr*10000+jmn*100+jdy
      price(i)=abs(price(i))
      if(nep(i).eq.nep(1)) go to 1
5     icnt=i-1
      if(icnt.lt.50) go to 200
      if(nms(iperm(1)).eq.0) go to 18
      do 16 nn=1,nms(iperm(1))
          do 10 ii=1,icnt
              if(jdate(ii).ge.ndate(iperm(1),nn)) price(ii)=price(ii)*(1.+factr(iperm(1),nn))
10        continue
16    continue
18    icntm=icnt-1
      
      do 20 i1=1,icntm
          ip=i1+1
          do 15 i2=ip,icnt
              if(turno(i1).le.turno(i2)) go to 15
              tmp1=turno(i1)
              tmp2=price(i1)
              tmp3=favg(i1)
              turno(i1)=turno(i2)
              price(i1)=price(i2)
              favg(i1)=favg(i2)
              turno(i2)=tmp1
              price(i2)=tmp2
              favg(i2)=tmp3
15        continue
20    continue
      avgprc=0.
      do 30 ii=1,icnt
30    avgprc=avgprc+price(ii)/float(icnt)
      rcnt5=(float(icnt)+.0001)/5.
      icnt5=int(rcnt5)
      idif=icnt-5*icnt5
      avgt=0.
      avgp=0.
      avgf=0.
      cnt=0.
      do 50 k=1,2
         ibg=(k-1)*icnt5+1
         iend=k*icnt5
         iendm=iend-1
         do 40 i1=ibg,iendm
             ip=i1+1
             do 35 i2=ip,iend
              if(favg(i1).le.favg(i2)) go to 35
              tmp1=turno(i1)
              tmp2=price(i1)
              tmp3=favg(i1)
              turno(i1)=turno(i2)
              price(i1)=price(i2)
              favg(i1)=favg(i2)
              turno(i2)=tmp1
              price(i2)=tmp2
              favg(i2)=tmp3
35           continue
40       continue
         rncnt5=(float(icnt5)+.0001)/5.
         ncnt5=int(rncnt5)
         ndif=icnt5-5*ncnt5
         do 48 n=1,5
             if(n.le.3) nbg=(n-1)*ncnt5+ibg
             if(n.le.2) nend=n*ncnt5+ibg-1
             if(n.ge.3) nend=n*ncnt5+ndif+ibg-1
             if(n.ge.4) nbg=(n-1)*ncnt5+ndif+ibg
             rcnt=float(nend)-float(nbg)+1.
            do 45 nn=nbg,nend
                avgt(k,n)=avgt(k,n)+turno(nn)/rcnt
                avgp(k,n)=avgp(k,n)+price(nn)/(rcnt*avgprc)
                avgf(k,n)=avgf(k,n)+favg(nn)/rcnt
                cnt(k,n)=rcnt
45          continue
48       continue
50    continue         
            
         ibg=(3-1)*icnt5+1
         iend=3*icnt5+idif
         iendm=iend-1
         icnt3rd=iend-ibg+1
         do 60 i1=ibg,iendm
             ip=i1+1
             do 55 i2=ip,iend
              if(favg(i1).le.favg(i2)) go to 55
              tmp1=turno(i1)
              tmp2=price(i1)
              tmp3=favg(i1)
              turno(i1)=turno(i2)
              price(i1)=price(i2)
              favg(i1)=favg(i2)
              turno(i2)=tmp1
              price(i2)=tmp2
              favg(i2)=tmp3
55           continue
60       continue
         rncnt5=(float(icnt3rd)+.0001)/5.
         ncnt5=int(rncnt5)
         ndif=icnt3rd-5*ncnt5
         do 68 n=1,5
             if(n.le.3) nbg=(n-1)*ncnt5+ibg
             if(n.le.2) nend=n*ncnt5+ibg-1
             if(n.ge.3) nend=n*ncnt5+ndif+ibg-1
             if(n.ge.4) nbg=(n-1)*ncnt5+ndif+ibg
             rcnt=float(nend)-float(nbg)+1.
            do 65 nn=nbg,nend
                avgt(k,n)=avgt(k,n)+turno(nn)/rcnt
                avgp(k,n)=avgp(k,n)+price(nn)/(rcnt*avgprc)
                avgf(k,n)=avgf(k,n)+favg(nn)/rcnt
                cnt(k,n)=rcnt
65          continue
68       continue
            

         do 90 k=4,5
         ibg=(k-1)*icnt5+1+idif
         iend=k*icnt5+idif
         iendm=iend-1
         do 80 i1=ibg,iendm
             ip=i1+1
             do 75 i2=ip,iend
              if(favg(i1).le.favg(i2)) go to 75
              tmp1=turno(i1)
              tmp2=price(i1)
              tmp3=favg(i1)
              turno(i1)=turno(i2)
              price(i1)=price(i2)
              favg(i1)=favg(i2)
              turno(i2)=tmp1
              price(i2)=tmp2
              favg(i2)=tmp3
75           continue
80       continue
         rncnt5=(float(icnt5)+.0001)/5.
         ncnt5=int(rncnt5)
         ndif=icnt5-5*ncnt5
         do 88 n=1,5
             if(n.le.3) nbg=(n-1)*ncnt5+ibg
             if(n.le.2) nend=n*ncnt5+ibg-1
             if(n.ge.3) nend=n*ncnt5+ndif+ibg-1
             if(n.ge.4) nbg=(n-1)*ncnt5+ndif+ibg
             rcnt=float(nend)-float(nbg)+1.
            do 85 nn=nbg,nend
                avgt(k,n)=avgt(k,n)+turno(nn)/rcnt
                avgp(k,n)=avgp(k,n)+price(nn)/(rcnt*avgprc)
                avgf(k,n)=avgf(k,n)+favg(nn)/rcnt
                cnt(k,n)=rcnt
85          continue
88       continue
90       continue         

         do 110 k=1,5
             do 100 n=1,5
                 write(99,9900)nep(1),k,n,cnt(k,n),avgt(k,n),avgf(k,n),avgp(k,n)
                 write(6,9900)nep(1),k,n,cnt(k,n),avgt(k,n),avgf(k,n),avgp(k,n)
9900             format(1x,i4,2(1x,i1),1x,f5.0,1x,f9.6,1x,f11.4,1x,f9.2)
100          continue
110      continue
         
      
      
200   nep(1)=nep(i)
      favg(1)=favg(i)
      iperm(1)=iperm(i)
      price(1)=price(i)
      turno(1)=turno(i)
      jdate(1)=jdate(i)
      i=1
      go to 1

900   continue
      stop
    end program turn_fee_5x5
    